package com.tosmart.jorm.tools;

import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author tosmart@gmail.com
 * @see DBHelper
 */
public class ConnectionPool {

    public static void config(String fileName) {
        try {
            JAXPConfigurator.configure(fileName, false);
            prepared = true;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(String alias) {

        if (!prepared) {
            throw new RuntimeException(
                    "getConnection: Connection Not Configed.");
        }

        final String url = "proxool." + alias;

        try {
            return DriverManager.getConnection(url);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private static boolean prepared;
}
